1 00:00:05,930 --> 00:00:03,590 hey this is my quest of medibang gorg if 2 00:00:07,610 --> 00:00:05,940 you think my videos are a bit too 3 00:00:09,080 --> 00:00:07,620 technical sometimes then you should 4 00:00:10,339 --> 00:00:09,090 probably stop watching now because this 5 00:00:13,009 --> 00:00:10,349 is going to be a very very very 6 00:00:16,220 --> 00:00:13,019 technical video what I'm going to do is 7 00:00:18,680 --> 00:00:16,230 I'm going to explain how this refraction 8 00:00:20,450 --> 00:00:18,690 simulator that I wrote actually works 9 00:00:22,790 --> 00:00:20,460 I'm going to have a look at the source 10 00:00:25,460 --> 00:00:22,800 code and the mathematics and the 11 00:00:27,769 --> 00:00:25,470 equations behind it and I'll try to do 12 00:00:29,450 --> 00:00:27,779 it reasonably quickly so you can just 13 00:00:31,130 --> 00:00:29,460 look at the actual stuff yourself 14 00:00:35,950 --> 00:00:31,140 hopefully I should give you enough of an 15 00:00:38,930 --> 00:00:35,960 understanding so refraction simulator is 16 00:00:41,660 --> 00:00:38,940 basically simulating the effects of 17 00:00:45,619 --> 00:00:41,670 atmospheric refraction and you see here 18 00:00:48,319 --> 00:00:45,629 we have rays of light which are coming 19 00:00:51,410 --> 00:00:48,329 from the camera this is using the 20 00:00:52,910 --> 00:00:51,420 principle of reversibility of light goes 21 00:00:55,130 --> 00:00:52,920 exactly the same way through air 22 00:00:56,569 --> 00:00:55,140 regardless of whether you go this way or 23 00:00:59,150 --> 00:00:56,579 this way now of course light is actually 24 00:01:01,010 --> 00:00:59,160 going from the target to the camera but 25 00:01:02,599 --> 00:01:01,020 we can actually cast rays from the 26 00:01:07,520 --> 00:01:02,609 camera to the target and it goes along 27 00:01:10,700 --> 00:01:07,530 the same path so what we want to do in 28 00:01:13,460 --> 00:01:10,710 terms of code is that we start out from 29 00:01:17,390 --> 00:01:13,470 a position over here where the camera is 30 00:01:19,550 --> 00:01:17,400 and we give the Ray a direction which 31 00:01:20,960 --> 00:01:19,560 and it's like in tentacle now I use a 32 00:01:22,969 --> 00:01:20,970 two-dimensional vector 33 00:01:25,550 --> 00:01:22,979 I just use a two-dimensional unit vector 34 00:01:27,770 --> 00:01:25,560 which is a vector of length 1 and that 35 00:01:31,370 --> 00:01:27,780 specifies which way the line is pointing 36 00:01:34,010 --> 00:01:31,380 and I store its position as another 2d 37 00:01:35,660 --> 00:01:34,020 vector and X Y vector where X is the 38 00:01:39,950 --> 00:01:35,670 horizontal distance and Y is the 39 00:01:43,730 --> 00:01:39,960 vertical distance 0 0 in this situation 40 00:01:45,440 --> 00:01:43,740 is the ground beneath the feet of the 41 00:01:47,899 --> 00:01:45,450 where the guy with a camera is 42 00:01:52,550 --> 00:01:47,909 theoretically sending so if we move in 43 00:01:55,850 --> 00:01:52,560 way up here 0 0 in our coordinate system 44 00:02:01,020 --> 00:01:55,860 is this position right here and so this 45 00:02:06,510 --> 00:02:03,860 the nuts and bolts of the code are 46 00:02:08,460 --> 00:02:06,520 tracing a ray which means we start with 47 00:02:12,300 --> 00:02:08,470 a ray a certain position which we call P 48 00:02:14,690 --> 00:02:12,310 and we have a direction which I specify 49 00:02:17,370 --> 00:02:14,700 with a unit vector which I call you and 50 00:02:22,199 --> 00:02:17,380 I trace it forward one step at a time 51 00:02:24,330 --> 00:02:22,209 along a distance D and we move through 52 00:02:25,800 --> 00:02:24,340 this medium which is a varying 53 00:02:29,370 --> 00:02:25,810 refractive index and the actual 54 00:02:32,160 --> 00:02:29,380 refractive index affects how the ray 55 00:02:34,560 --> 00:02:32,170 actually curves so we move it a step 56 00:02:35,970 --> 00:02:34,570 forward we see as it moved up or down a 57 00:02:38,010 --> 00:02:35,980 little bit and then we move it up or 58 00:02:39,240 --> 00:02:38,020 down and then we move it again and we 59 00:02:41,420 --> 00:02:39,250 keep moving and eventually we'll hit 60 00:02:45,570 --> 00:02:41,430 something and then where we hit it 61 00:02:49,190 --> 00:02:45,580 determines what red watch line of the 62 00:02:51,540 --> 00:02:49,200 image ends up in the final result so 63 00:02:54,449 --> 00:02:51,550 probably lost you all already but I'm 64 00:02:57,180 --> 00:02:54,459 gonna plow on now if you want to see how 65 00:03:01,710 --> 00:02:57,190 this simulator actually works then you 66 00:03:04,250 --> 00:03:01,720 have to look at the source code and here 67 00:03:07,470 --> 00:03:04,260 in chrome I can just go to view 68 00:03:09,509 --> 00:03:07,480 developer view source and the source 69 00:03:12,479 --> 00:03:09,519 code will pop up blah blah blah it's all 70 00:03:15,660 --> 00:03:12,489 there but actually a couple of other 71 00:03:19,500 --> 00:03:15,670 files which you can see listed at the 72 00:03:22,140 --> 00:03:19,510 top let's see they're gonna be right 73 00:03:23,820 --> 00:03:22,150 here you can click on them to just load 74 00:03:26,039 --> 00:03:23,830 them and then you can you can see them 75 00:03:29,460 --> 00:03:26,049 here but another way of doing this is in 76 00:03:32,490 --> 00:03:29,470 chrome you do view developer and 77 00:03:35,160 --> 00:03:32,500 developer tools and it will bring you up 78 00:03:36,600 --> 00:03:35,170 this little list neat neatly shows kind 79 00:03:38,759 --> 00:03:36,610 of the project structure and where the 80 00:03:42,240 --> 00:03:38,769 other files are jQuery is a strong that 81 00:03:44,370 --> 00:03:42,250 standard library library the action most 82 00:03:48,870 --> 00:03:44,380 of the code is actually in the index 83 00:03:50,610 --> 00:03:48,880 file index.html and most of the actual 84 00:03:54,420 --> 00:03:50,620 interesting code is in there some other 85 00:03:56,490 --> 00:03:54,430 stuff is in this file here pseudo ID and 86 00:03:58,710 --> 00:03:56,500 refraction GS which has a few equations 87 00:04:00,810 --> 00:03:58,720 for calculating refractive index then 88 00:04:03,720 --> 00:04:00,820 the curve editor which is a thing I 89 00:04:05,370 --> 00:04:03,730 wrote to easily edit the actual curves 90 00:04:07,800 --> 00:04:05,380 it's not very complicated it's just 91 00:04:10,530 --> 00:04:07,810 Bezier curves which I guess is 92 00:04:12,479 --> 00:04:10,540 complicated but you know that's these 93 00:04:13,660 --> 00:04:12,489 are the only extra bits of code that are 94 00:04:15,400 --> 00:04:13,670 relevant but 95 00:04:17,830 --> 00:04:15,410 I wrote this is just an implementation 96 00:04:19,810 --> 00:04:17,840 of standard equations and this is just 97 00:04:22,680 --> 00:04:19,820 editing a curve right back to the index 98 00:04:25,900 --> 00:04:22,690 I'm actually going to show you this in 99 00:04:27,700 --> 00:04:25,910 the program I use which piece phpstorm 100 00:04:31,030 --> 00:04:27,710 and I've made the font nice and big so 101 00:04:33,190 --> 00:04:31,040 you can see you can see what what's 102 00:04:35,980 --> 00:04:33,200 actually going on so the heart of the 103 00:04:39,879 --> 00:04:35,990 program is this one function here trace 104 00:04:43,780 --> 00:04:39,889 one line and it's taking a line number 105 00:04:47,590 --> 00:04:43,790 which is actually the line number of the 106 00:04:50,890 --> 00:04:47,600 the actual target image which would be 107 00:04:52,750 --> 00:04:50,900 when I was the the resultant image the 108 00:04:56,830 --> 00:04:52,760 rendered image so the line on the screen 109 00:04:59,020 --> 00:04:56,840 basically so it's my life checking to 110 00:05:01,450 --> 00:04:59,030 see if it's being done already is it 111 00:05:04,270 --> 00:05:01,460 just things asynchronously so it will do 112 00:05:05,710 --> 00:05:04,280 like every one out of ten lines and then 113 00:05:08,710 --> 00:05:05,720 it Flags them as being done and there's 114 00:05:10,600 --> 00:05:08,720 an array called rays which contains all 115 00:05:12,730 --> 00:05:10,610 the lines and as a bunch of bunch of 116 00:05:14,800 --> 00:05:12,740 flags in but not that difficult 117 00:05:16,330 --> 00:05:14,810 not that complicated but you can look at 118 00:05:17,710 --> 00:05:16,340 the actual structure of the rays array 119 00:05:19,180 --> 00:05:17,720 and see the various of the four things 120 00:05:21,190 --> 00:05:19,190 in it 121 00:05:27,880 --> 00:05:21,200 we can I collect the angle of the Ray 122 00:05:29,140 --> 00:05:27,890 and now obviously when when array is 123 00:05:30,909 --> 00:05:29,150 leaving your eye goes out at a 124 00:05:33,040 --> 00:05:30,919 particular angle zero is going to be 125 00:05:35,110 --> 00:05:33,050 perfectly horizontal and this is going 126 00:05:37,450 --> 00:05:35,120 to be up at whatever the field of view 127 00:05:39,070 --> 00:05:37,460 is which is 1.5 degrees here remember 128 00:05:40,360 --> 00:05:39,080 this is greatly exaggerated so it looks 129 00:05:42,190 --> 00:05:40,370 like it's a fairly wide field of view 130 00:05:47,950 --> 00:05:42,200 it's only actually 1.5 degrees which is 131 00:05:50,710 --> 00:05:47,960 a very very narrow field of view it's 132 00:05:53,590 --> 00:05:50,720 taking x and y's being the star position 133 00:05:54,880 --> 00:05:53,600 there's just the the the eye x and y 134 00:05:59,740 --> 00:05:54,890 which is the basically the camera 135 00:06:02,170 --> 00:05:59,750 position and it takes this angle or 136 00:06:03,310 --> 00:06:02,180 whatever it's doing there I guess it's 137 00:06:05,350 --> 00:06:03,320 just check checking to see if you're 138 00:06:06,659 --> 00:06:05,360 going up or down coloring the Lions red 139 00:06:10,390 --> 00:06:06,669 or blue 140 00:06:13,180 --> 00:06:10,400 this is detecting which of the lines is 141 00:06:15,310 --> 00:06:13,190 eye level not very important really just 142 00:06:16,810 --> 00:06:15,320 cosmetic thing and here this is where we 143 00:06:22,810 --> 00:06:16,820 get to some of the actual mathematics 144 00:06:27,040 --> 00:06:22,820 here getting the value D to be the step 145 00:06:30,790 --> 00:06:27,050 along the the line which is how 146 00:06:33,939 --> 00:06:30,800 we're gonna go in one step in our ray 147 00:06:35,649 --> 00:06:33,949 tracing algorithm which is typically the 148 00:06:42,399 --> 00:06:35,659 distance to the target and divided by 149 00:06:47,339 --> 00:06:42,409 500 I think now we have the unit vector 150 00:06:50,559 --> 00:06:47,349 U which is the direction of the Ray and 151 00:06:53,770 --> 00:06:50,569 we just use two variables u ax and UI to 152 00:06:55,149 --> 00:06:53,780 store this and we calculating it simply 153 00:06:57,640 --> 00:06:55,159 as being the cosine and the negative 154 00:06:59,950 --> 00:06:57,650 sine of the angle and you want to think 155 00:07:05,589 --> 00:06:59,960 about how that actually works you can 156 00:07:09,610 --> 00:07:05,599 think of a actual vector d DX and dy 157 00:07:12,969 --> 00:07:09,620 which is the the length of D multiplied 158 00:07:15,459 --> 00:07:12,979 by the cosine and the negative sine of 159 00:07:16,809 --> 00:07:15,469 the angle and then you calculate the 160 00:07:18,430 --> 00:07:16,819 length of that and then you calculate 161 00:07:21,219 --> 00:07:18,440 the unit vector by dividing it by the 162 00:07:23,320 --> 00:07:21,229 length so this gives you the actual off 163 00:07:26,439 --> 00:07:23,330 certainty but for our purposes we're 164 00:07:28,029 --> 00:07:26,449 only using the unit vector so we don't 165 00:07:29,680 --> 00:07:28,039 need to have these intermediate values 166 00:07:33,390 --> 00:07:29,690 which you might if you're doing it in a 167 00:07:37,570 --> 00:07:33,400 slightly different way all right 168 00:07:40,899 --> 00:07:37,580 so for this Ray we are going to build in 169 00:07:43,450 --> 00:07:40,909 an array of line points which is just 170 00:07:45,040 --> 00:07:43,460 point XY point and we're going to check 171 00:07:47,409 --> 00:07:45,050 to see if we hit the ground and we check 172 00:07:50,140 --> 00:07:47,419 to see if we recheck later to see if we 173 00:07:52,390 --> 00:07:50,150 hit the target this is how much we're 174 00:07:55,629 --> 00:07:52,400 actually going to trace we just 175 00:07:57,459 --> 00:07:55,639 basically trace the amount that is 176 00:08:00,809 --> 00:07:57,469 visible in this diagram here so we'll 177 00:08:03,100 --> 00:08:00,819 trace all the way over to here to see if 178 00:08:05,170 --> 00:08:03,110 you know we hit something we don't we 179 00:08:06,740 --> 00:08:05,180 just basically stop if we get it to this 180 00:08:11,090 --> 00:08:06,750 point 181 00:08:13,370 --> 00:08:11,100 and if if it's a flat earth then we 182 00:08:15,220 --> 00:08:13,380 actually triple the amount that we trace 183 00:08:17,570 --> 00:08:15,230 because we want to see the horizon 184 00:08:19,660 --> 00:08:17,580 beyond things this isn't work entirely 185 00:08:22,190 --> 00:08:19,670 right and I need to improve that but 186 00:08:25,760 --> 00:08:22,200 that's why I'm doing that try to get a 187 00:08:31,190 --> 00:08:25,770 more accurate horizon beyond the actual 188 00:08:34,159 --> 00:08:31,200 target ok so this is the main loop that 189 00:08:36,800 --> 00:08:34,169 loops and calculates all of the points 190 00:08:40,700 --> 00:08:36,810 in the ray tracing this this function 191 00:08:42,110 --> 00:08:40,710 call here is the key thing and which 192 00:08:45,590 --> 00:08:42,120 will need the most explanation 193 00:08:48,170 --> 00:08:45,600 it takes us parameters x and y which is 194 00:08:50,960 --> 00:08:48,180 the current position it takes UX and uy 195 00:08:52,670 --> 00:08:50,970 which is the direction and it takes D 196 00:08:54,860 --> 00:08:52,680 which is how much of a step we're going 197 00:08:57,800 --> 00:08:54,870 to take so D times you actually uy will 198 00:08:59,980 --> 00:08:57,810 give you the new position and it calls 199 00:09:02,540 --> 00:08:59,990 this function here step forward and 200 00:09:05,260 --> 00:09:02,550 updates all the values if it hits the 201 00:09:09,110 --> 00:09:05,270 ground then it Flags it and then stops 202 00:09:10,580 --> 00:09:09,120 otherwise it adds a new point XY to the 203 00:09:13,190 --> 00:09:10,590 array of line points and just keeps 204 00:09:14,990 --> 00:09:13,200 going until this islet done or its hit 205 00:09:18,260 --> 00:09:15,000 hit the ground by done being going off 206 00:09:20,690 --> 00:09:18,270 the right hand side and that's pretty 207 00:09:22,520 --> 00:09:20,700 much it for that so let's delve into 208 00:09:25,220 --> 00:09:22,530 what step forward does this is the 209 00:09:27,380 --> 00:09:25,230 complicated bit if you don't think it's 210 00:09:30,370 --> 00:09:27,390 complicated enough already so I'm going 211 00:09:33,050 --> 00:09:30,380 to zip into that function step forward 212 00:09:35,540 --> 00:09:33,060 it's planes wait I see a given x and y 213 00:09:41,810 --> 00:09:35,550 we're going to step forward by x times d 214 00:09:43,820 --> 00:09:41,820 uy times d and blah blah blah so to 215 00:09:45,560 --> 00:09:43,830 explain how this works there's a venture 216 00:09:46,880 --> 00:09:45,570 this is what it actually does here to 217 00:09:50,140 --> 00:09:46,890 explain how this works I'm going to have 218 00:09:54,470 --> 00:09:50,150 to show you a diagram 219 00:10:00,530 --> 00:09:58,520 okay now if every country this base has 220 00:10:05,180 --> 00:10:00,540 simple integration of a point P of 221 00:10:10,100 --> 00:10:05,190 distance D in Direction u here's a point 222 00:10:11,960 --> 00:10:10,110 P the point P is the point on a ray of 223 00:10:15,560 --> 00:10:11,970 light but really you've got to think of 224 00:10:17,660 --> 00:10:15,570 the motion of light as being the motion 225 00:10:20,030 --> 00:10:17,670 of a wavefront you see this squiggly 226 00:10:23,030 --> 00:10:20,040 line here it's meant to be a wavefront 227 00:10:26,180 --> 00:10:23,040 so it's the the ray of light is actually 228 00:10:29,660 --> 00:10:26,190 just one point on a wave that's going 229 00:10:32,050 --> 00:10:29,670 along and we have this point P on that 230 00:10:36,620 --> 00:10:32,060 wave and we want to know if it's gone up 231 00:10:40,160 --> 00:10:36,630 distance D along direction vector u what 232 00:10:42,770 --> 00:10:40,170 will be the new point P - which is 233 00:10:45,290 --> 00:10:42,780 sometimes called P Prime but old British 234 00:10:48,800 --> 00:10:45,300 so I'm calling it P - and what will be 235 00:10:51,740 --> 00:10:48,810 the new direction u - so the mathematics 236 00:10:56,000 --> 00:10:51,750 we're doing here is how do we take P and 237 00:10:58,010 --> 00:10:56,010 you and turn it into P - and you - so 238 00:11:00,980 --> 00:10:58,020 the old way I did it was that I would 239 00:11:03,860 --> 00:11:00,990 calculates two points a and B on either 240 00:11:07,010 --> 00:11:03,870 side a B either side of p on this 241 00:11:09,530 --> 00:11:07,020 wavefront then I would calculate the 242 00:11:13,610 --> 00:11:09,540 points a dash and B dash and the way I 243 00:11:15,230 --> 00:11:13,620 do that is that a and B will be in 244 00:11:18,260 --> 00:11:15,240 different parts of the medium so they're 245 00:11:20,990 --> 00:11:18,270 in different refractive indices the 246 00:11:23,860 --> 00:11:21,000 refractive index will be different for a 247 00:11:28,430 --> 00:11:23,870 and B so it'll move at different speeds 248 00:11:31,550 --> 00:11:28,440 through through this the speed of light 249 00:11:33,260 --> 00:11:31,560 in a medium is inversely proportional to 250 00:11:35,750 --> 00:11:33,270 the refractive index so you have to 251 00:11:38,480 --> 00:11:35,760 divide the speed of light by the 252 00:11:39,800 --> 00:11:38,490 refractive index and that gives you the 253 00:11:41,060 --> 00:11:39,810 actual speed of light and that tell you 254 00:11:44,210 --> 00:11:41,070 how far it can go 255 00:11:45,740 --> 00:11:44,220 so this point P will move based on 256 00:11:48,350 --> 00:11:45,750 whatever the refractive index is here 257 00:11:50,060 --> 00:11:48,360 this point a will move based on whether 258 00:11:52,250 --> 00:11:50,070 the refractive index is here and end up 259 00:11:54,170 --> 00:11:52,260 in a - so you could calculate these two 260 00:11:57,350 --> 00:11:54,180 points a and B's and calculate some new 261 00:11:59,270 --> 00:11:57,360 points a dash and B dash and then that 262 00:12:01,670 --> 00:11:59,280 would give you this line here and you 263 00:12:03,470 --> 00:12:01,680 can use this line here to find out what 264 00:12:04,790 --> 00:12:03,480 the new you - would be because it's 265 00:12:07,850 --> 00:12:04,800 simply the vector which is perpendicular 266 00:12:11,900 --> 00:12:07,860 to this line 267 00:12:14,299 --> 00:12:11,910 I do us something of a an optimization 268 00:12:17,629 --> 00:12:14,309 or a simplification here instead of 269 00:12:23,199 --> 00:12:17,639 calculating these points a and B I just 270 00:12:28,220 --> 00:12:23,209 directly calculate the value of U from 271 00:12:32,809 --> 00:12:28,230 this value G where G is the unit 272 00:12:37,389 --> 00:12:32,819 gradient of the refractive index field 273 00:12:41,869 --> 00:12:37,399 at P that's the UM perhaps a little bit 274 00:12:44,809 --> 00:12:41,879 hard to understand like I said it's from 275 00:12:47,569 --> 00:12:44,819 your brother hard to understand but the 276 00:12:51,519 --> 00:12:47,579 refractive index gradient that is how 277 00:12:57,079 --> 00:12:51,529 much is how much is the value of N 278 00:12:58,970 --> 00:12:57,089 changing in the X direction and in the 279 00:13:05,749 --> 00:12:58,980 in the X direction and in the Y 280 00:13:08,569 --> 00:13:05,759 direction per foot and that is a value G 281 00:13:11,239 --> 00:13:08,579 which is a vector value obviously to GX 282 00:13:12,650 --> 00:13:11,249 and Gy because it's how much is it 283 00:13:14,269 --> 00:13:12,660 changing X and what's is changing and 284 00:13:16,579 --> 00:13:14,279 why it's represented by an arrow here 285 00:13:19,929 --> 00:13:16,589 because it's going to be a direction it 286 00:13:21,980 --> 00:13:19,939 will be not necessarily a unit vector 287 00:13:24,079 --> 00:13:21,990 because it will have a certain length 288 00:13:31,189 --> 00:13:24,089 and the steeper the gradient the longer 289 00:13:33,889 --> 00:13:31,199 this this G will be so the meth is not 290 00:13:36,259 --> 00:13:33,899 too long but it's perhaps difficult to 291 00:13:37,999 --> 00:13:36,269 understand so let's go through it U is 292 00:13:39,350 --> 00:13:38,009 the unit vector or the rate erection we 293 00:13:42,199 --> 00:13:39,360 already know that it's this value here 294 00:13:45,139 --> 00:13:42,209 it's the unit vector along this this 295 00:13:47,840 --> 00:13:45,149 line of travel P is the current rate 296 00:13:49,549 --> 00:13:47,850 where are we starting a and B are two 297 00:13:54,439 --> 00:13:49,559 points on the way from perpendicular to 298 00:13:56,119 --> 00:13:54,449 u which they're going to be virtual 299 00:13:59,809 --> 00:13:56,129 points but right now imagine they are 300 00:14:02,509 --> 00:13:59,819 actually real points V now I see V as a 301 00:14:04,879 --> 00:14:02,519 - as a little underscore which means 302 00:14:08,059 --> 00:14:04,889 that it's a vector quantity V is the 303 00:14:10,340 --> 00:14:08,069 left rotated you so here's you and 304 00:14:14,600 --> 00:14:10,350 here's V it's rotated counterclockwise 305 00:14:18,110 --> 00:14:14,610 to the left by 90 degrees so it's the 306 00:14:20,480 --> 00:14:18,120 vector perpendicular to - you 307 00:14:24,079 --> 00:14:20,490 is the refractive index at P so it's the 308 00:14:29,299 --> 00:14:24,089 refractive index at this position here G 309 00:14:31,100 --> 00:14:29,309 as with it is the gradient of n at P now 310 00:14:34,009 --> 00:14:31,110 this is just explaining how you 311 00:14:36,860 --> 00:14:34,019 calculate rotated right and rotated left 312 00:14:41,210 --> 00:14:36,870 values rotate right it's just simply hue 313 00:14:43,280 --> 00:14:41,220 I minus X and rotate left is minus y 314 00:14:44,509 --> 00:14:43,290 minus X and let's explain why there's a 315 00:14:47,780 --> 00:14:44,519 little diagram here I should be fairly 316 00:14:50,629 --> 00:14:47,790 straightforward alright so as we have 317 00:14:54,410 --> 00:14:50,639 all these values we can figure out 318 00:14:58,670 --> 00:14:54,420 certain things we know that a is P plus 319 00:15:01,910 --> 00:14:58,680 B we're going to use an offset of 1 so 320 00:15:04,369 --> 00:15:01,920 we just use the unit vector to get the 321 00:15:05,989 --> 00:15:04,379 natural position of a and the B of 322 00:15:07,670 --> 00:15:05,999 course is P minus V because it's just in 323 00:15:15,170 --> 00:15:07,680 the other direction then we can 324 00:15:25,299 --> 00:15:15,180 calculate a dash as people's v8 a she's 325 00:15:29,419 --> 00:15:25,309 a plus u times D multiplied by n plus GB 326 00:15:32,169 --> 00:15:29,429 this is this is very frank it's a little 327 00:15:38,210 --> 00:15:32,179 confused but basically what that does is 328 00:15:42,259 --> 00:15:38,220 it takes the position a and he adds u 329 00:15:45,319 --> 00:15:42,269 times D scaled by a you deal with this 330 00:15:49,100 --> 00:15:45,329 lengthier from P - P - scaled by the 331 00:15:54,980 --> 00:15:49,110 refractive index at P which is n plus G 332 00:16:00,139 --> 00:15:54,990 dot V which is the component of G along 333 00:16:01,999 --> 00:16:00,149 V and which gives you how much the 334 00:16:06,079 --> 00:16:02,009 refractive index is changing in this 335 00:16:08,269 --> 00:16:06,089 direction from P to a and similarly we 336 00:16:11,629 --> 00:16:08,279 do the same thing for our B - we take B 337 00:16:15,590 --> 00:16:11,639 and we add u times D which is this 338 00:16:19,220 --> 00:16:15,600 vector scaled by n - G dot V which is 339 00:16:22,340 --> 00:16:19,230 going to be how much G varies in this 340 00:16:25,400 --> 00:16:22,350 direction so that will actually give you 341 00:16:29,030 --> 00:16:25,410 a virtual a and B so now we can 342 00:16:31,820 --> 00:16:29,040 calculate another vector B - a - which 343 00:16:35,840 --> 00:16:31,830 is this one here which is 344 00:16:37,550 --> 00:16:35,850 to be a - - B - which if you take these 345 00:16:41,840 --> 00:16:37,560 just works out to be something nice 346 00:16:46,490 --> 00:16:41,850 which doesn't involve a or b so - v + 2 347 00:16:49,250 --> 00:16:46,500 UD times govt and we can work out quite 348 00:16:51,350 --> 00:16:49,260 simply that you - which is the new unit 349 00:16:56,540 --> 00:16:51,360 vector in this direction is the right 350 00:16:58,340 --> 00:16:56,550 rotated vector B 2 a so it is B 2 a is 351 00:17:00,080 --> 00:16:58,350 that vector and then we were setting it 352 00:17:01,970 --> 00:17:00,090 right 90 degrees and then we take the 353 00:17:03,710 --> 00:17:01,980 unit vector which is what I'm indicating 354 00:17:07,100 --> 00:17:03,720 with these bars on either side and that 355 00:17:09,560 --> 00:17:07,110 gives you a new direction vector which 356 00:17:13,460 --> 00:17:09,570 gives us this nice little equation here 357 00:17:19,180 --> 00:17:13,470 you - you write rotated V + UD terms 358 00:17:24,140 --> 00:17:19,190 govt which slightly simplifies to this 359 00:17:27,740 --> 00:17:24,150 you - is U plus V times D times G dot V 360 00:17:33,560 --> 00:17:27,750 because the U and V are the right 361 00:17:36,470 --> 00:17:33,570 rotated versions of V and u then we have 362 00:17:38,630 --> 00:17:36,480 simply P - is P Plus u times D now we 363 00:17:42,080 --> 00:17:38,640 could multiply that by n times P but it 364 00:17:43,400 --> 00:17:42,090 doesn't really matter because the it 365 00:17:44,660 --> 00:17:43,410 doesn't really make much difference but 366 00:17:46,880 --> 00:17:44,670 I think I do actually do this very much 367 00:17:48,470 --> 00:17:46,890 not sure but if you can introduce this 368 00:17:51,110 --> 00:17:48,480 term into the code it doesn't really 369 00:17:52,880 --> 00:17:51,120 make very much difference all right now 370 00:17:56,030 --> 00:17:52,890 if thoroughly confuse everybody we can 371 00:17:58,660 --> 00:17:56,040 go back to the code and we can see that 372 00:18:02,810 --> 00:17:58,670 what we have is just simply an 373 00:18:07,580 --> 00:18:02,820 implementation of those equations we 374 00:18:11,930 --> 00:18:07,590 have NeoGeo v DG v and then you witness 375 00:18:16,750 --> 00:18:11,940 the new ux 1 UI 1 which is the work you 376 00:18:18,860 --> 00:18:16,760 do for you - calculated here and then 377 00:18:20,960 --> 00:18:18,870 normalizing it here by dividing by the 378 00:18:23,270 --> 00:18:20,970 length and then returning it and doing 379 00:18:26,480 --> 00:18:23,280 at x equals ya I don't actually add in 380 00:18:27,800 --> 00:18:26,490 the refractive index - you can you try 381 00:18:29,510 --> 00:18:27,810 it with them without but it doesn't 382 00:18:31,930 --> 00:18:29,520 actually make any difference so I left 383 00:18:36,550 --> 00:18:31,940 it off for speed 384 00:18:39,790 --> 00:18:36,560 ok that is how that works now 385 00:18:41,650 --> 00:18:39,800 I must admit it's very complicated 386 00:18:44,050 --> 00:18:41,660 sounding I wouldn't expect most people 387 00:18:47,170 --> 00:18:44,060 to understand it but if you want to 388 00:18:49,630 --> 00:18:47,180 understand how the code works then 389 00:18:51,360 --> 00:18:49,640 hopefully that would lead you through it 390 00:18:55,750 --> 00:18:51,370 and you can go through it step by step 391 00:18:57,870 --> 00:18:55,760 and follow along with what I just did 392 00:19:00,690 --> 00:18:57,880 and check the math and then see that 393 00:19:08,920 --> 00:19:00,700 these equations hearings step forward 394 00:19:13,060 --> 00:19:08,930 match that math okay so other things in 395 00:19:14,560 --> 00:19:13,070 the code that are math related are how 396 00:19:19,030 --> 00:19:14,570 do we actually calculate the refractive 397 00:19:24,370 --> 00:19:19,040 index the refractive index is calculated 398 00:19:26,020 --> 00:19:24,380 let's see in this function somewhere 399 00:19:27,520 --> 00:19:26,030 here it uses this function here gradient 400 00:19:29,770 --> 00:19:27,530 because we're using the gradient of the 401 00:19:32,350 --> 00:19:29,780 refractive index so how do we calculate 402 00:19:36,220 --> 00:19:32,360 the gradient of GX and Gy we go into 403 00:19:38,380 --> 00:19:36,230 that and we see gradient x and y is just 404 00:19:41,260 --> 00:19:38,390 simply taking the refractive index 405 00:19:42,880 --> 00:19:41,270 that's half a foot on either side and 406 00:19:46,030 --> 00:19:42,890 then returning the difference between 407 00:19:48,310 --> 00:19:46,040 them and that gives you the gradient for 408 00:19:51,040 --> 00:19:48,320 the X and the gradient for the Y so it's 409 00:19:53,020 --> 00:19:51,050 very very simple really so what does 410 00:19:55,990 --> 00:19:53,030 this function do refractive index let's 411 00:19:58,270 --> 00:19:56,000 pop into that one I call this function 412 00:20:02,320 --> 00:19:58,280 here which is refractive index from H 413 00:20:05,140 --> 00:20:02,330 and that function here is always a it's 414 00:20:09,280 --> 00:20:05,150 a virtual function that it actually 415 00:20:11,890 --> 00:20:09,290 comes from this or this function here 416 00:20:14,950 --> 00:20:11,900 which calculates the function which 417 00:20:18,640 --> 00:20:14,960 ultimately calls this function here 418 00:20:20,530 --> 00:20:18,650 refractive index old which the oldest 419 00:20:24,580 --> 00:20:20,540 indicators before I did all this this 420 00:20:27,580 --> 00:20:24,590 fancy optimization and and or or they're 421 00:20:31,750 --> 00:20:27,590 doing a bit of a bit of a camel ROM 422 00:20:34,890 --> 00:20:31,760 spline interpolation down here which is 423 00:20:37,570 --> 00:20:34,900 an optimization so the actual relevant 424 00:20:40,630 --> 00:20:37,580 code for how we calculate the refractive 425 00:20:48,400 --> 00:20:40,640 index is in refractive index old which 426 00:20:49,850 --> 00:20:48,410 is right here which just takes the drop 427 00:20:52,430 --> 00:20:49,860 into account to calculate the height 428 00:20:55,130 --> 00:20:52,440 above the surface it calls pressure at 429 00:20:56,780 --> 00:20:55,140 altitude to calculate what's that 430 00:21:00,770 --> 00:20:56,790 pressure would be at that altitude which 431 00:21:05,840 --> 00:21:00,780 uses this equation here which comes from 432 00:21:09,919 --> 00:21:05,850 this Wikipedia page it calculates the 433 00:21:11,630 --> 00:21:09,929 temperature from the curve from this 434 00:21:13,130 --> 00:21:11,640 curve here you have to look in the it's 435 00:21:15,140 --> 00:21:13,140 just basically interpolating along this 436 00:21:21,620 --> 00:21:15,150 curve is very simple nothing complicated 437 00:21:25,190 --> 00:21:21,630 there it takes the RH from the default 438 00:21:27,799 --> 00:21:25,200 RH if you know editing the RH which will 439 00:21:29,960 --> 00:21:27,809 be 50 generally if you are aged in the 440 00:21:31,760 --> 00:21:29,970 RH it comes from this graph here and 441 00:21:34,520 --> 00:21:31,770 similarly it's interpolated along that 442 00:21:36,560 --> 00:21:34,530 curve and then the refractive index is 443 00:21:38,810 --> 00:21:36,570 calculated from that temperature and the 444 00:21:42,289 --> 00:21:38,820 pressure and the RH by calling this 445 00:21:45,919 --> 00:21:42,299 function here air index Sidor which if 446 00:21:49,610 --> 00:21:45,929 we go into that we can see is the full 447 00:21:51,380 --> 00:21:49,620 Sidor equation from NIST website which 448 00:21:53,710 --> 00:21:51,390 is sudo is well known in the scientific 449 00:21:55,850 --> 00:21:53,720 literature as being the most accurate 450 00:21:58,159 --> 00:21:55,860 equation but of course it's incredibly 451 00:21:59,630 --> 00:21:58,169 complicated there's actually other 452 00:22:01,370 --> 00:21:59,640 equations you can use in here there's 453 00:22:03,560 --> 00:22:01,380 the application which is a bit simpler 454 00:22:05,510 --> 00:22:03,570 or there's this missed shot fourth 455 00:22:08,419 --> 00:22:05,520 equation which is relatively simple and 456 00:22:12,740 --> 00:22:08,429 straightforward but that essentially is 457 00:22:13,430 --> 00:22:12,750 all the math that is relevant in in the 458 00:22:15,860 --> 00:22:13,440 simulator 459 00:22:18,230 --> 00:22:15,870 so if hopefully that will explain to 460 00:22:21,919 --> 00:22:18,240 people who want to know who put in the 461 00:22:24,380 --> 00:22:21,929 time how it actually works and if you 462 00:22:27,500 --> 00:22:24,390 want to replicate it or if you want to 463 00:22:29,270 --> 00:22:27,510 verify to if you want to improve it then 464 00:22:32,570 --> 00:22:29,280 it should give you a good understanding 465 00:22:33,860 --> 00:22:32,580 of the technical details behind it 466 00:22:35,470 --> 00:22:33,870 there's other stuff like how it does the 467 00:22:38,930 --> 00:22:35,480 rendering but that's just you know 468 00:22:40,789 --> 00:22:38,940 straightforward programming and just 469 00:22:44,080 --> 00:22:40,799 basically data management it's not 470 00:22:47,600 --> 00:22:44,090 really pretty interesting the actual 471 00:22:49,640 --> 00:22:47,610 technical stuff is this stuff right here 472 00:22:51,530 --> 00:22:49,650 the mathematics the equations the 473 00:22:55,150 --> 00:22:51,540 implementation of the those equations 474 00:22:59,120 --> 00:22:55,160 and how we step through them with our 475 00:23:02,659 --> 00:22:59,130 gradient there's various issues with it 476 00:23:03,710 --> 00:23:02,669 like the step size the calculation of 477 00:23:07,970 --> 00:23:03,720 the gradient 478 00:23:10,610 --> 00:23:07,980 is based on a step of one and you know 479 00:23:13,909 --> 00:23:10,620 things could be improved and it's not 480 00:23:15,770 --> 00:23:13,919 the same as the the the formula that 481 00:23:17,960 --> 00:23:15,780 Andy Young and Walter bisland used which 482 00:23:22,070 --> 00:23:17,970 uses curved segments but that is based 483 00:23:23,600 --> 00:23:22,080 on a much simpler version of the 484 00:23:25,430 --> 00:23:23,610 refractive index essentially it's using 485 00:23:26,930 --> 00:23:25,440 this very simple calculation which 486 00:23:28,580 --> 00:23:26,940 doesn't really use the full suit or 487 00:23:30,560 --> 00:23:28,590 equation so even though it's more 488 00:23:32,690 --> 00:23:30,570 accurate in some ways and it's going 489 00:23:33,980 --> 00:23:32,700 through a curve it's less accurate in 490 00:23:36,020 --> 00:23:33,990 this using this very very simple 491 00:23:40,220 --> 00:23:36,030 equation which is more resembles more 492 00:23:42,950 --> 00:23:40,230 the NIST equation here alright so I'm 493 00:23:46,039 --> 00:23:42,960 going to sign off because I think I'm 494 00:23:47,600 --> 00:23:46,049 done for now so if anybody sat through